#
# Makefile for building crash shared object extensions
#
# Copyright (C) 2005, 2007, 2009, 2011, 2013 David Anderson
# Copyright (C) 2005, 2007, 2009, 2011, 2013 Red Hat, Inc. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# To build the extension shared objects in this directory, run 
# "make extensions" from the top-level directory.
#
# To add a new extension object, simply copy your module's .c file
# to this directory, and it will be built automatically using
# the "standard" compile line.  If that compile line does not 
# suffice, create a .mk file with the same prefix as the .c file,
# and that makefile will be invoked. 
# 
CC ?= gcc
CONTRIB_SO := $(patsubst %.c,%.so,$(wildcard *.c))

all: link_defs $(CONTRIB_SO)
	
link_defs:
	@rm -f defs.h
	@ln ../defs.h 

$(CONTRIB_SO): %.so: %.c defs.h
	@if [ -f $*.mk ]; then \
		$(MAKE) -f $*.mk; \
	else \
		grep -q '((constructor))' $*.c && { \
			echo "$(CC) -Wall -g -shared -rdynamic -o $@ $*.c -fPIC -D$(TARGET) $(TARGET_CFLAGS) $(GDB_FLAGS)"; \
			$(CC) -Wall -g -shared -rdynamic -o $@ $*.c -fPIC -D$(TARGET) $(TARGET_CFLAGS) $(GDB_FLAGS); \
		} || { \
			echo "$(CC) -Wall -g -nostartfiles -shared -rdynamic -o $@ $*.c -fPIC -D$(TARGET) $(TARGET_CFLAGS) $(GDB_FLAGS)"; \
			$(CC) -Wall -g -nostartfiles -shared -rdynamic -o $@ $*.c -fPIC -D$(TARGET) $(TARGET_CFLAGS) $(GDB_FLAGS); \
		}; \
	fi

clean:
	rm -f $(CONTRIB_SO)
	@for MAKEFILE in `grep -sl "^clean:" *.mk`; \
	  do $(MAKE) -f $$MAKEFILE clean; \
	done
